﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using PCMSofCC;
using PCMSofCC.BLL;

namespace PCMSofCC.Admin
{
    public partial class AssignSiteStep2 : System.Web.UI.Page
    {
        private Guid UserId;
        private DateTime Date;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
            {
                ListBox lbxUsers = (ListBox)PreviousPage.Master.FindControl("Main").FindControl("lbxUsers");
                UserId = new Guid(lbxUsers.SelectedValue);
                Calendar Calendar1 = (Calendar)PreviousPage.Master.FindControl("Main").FindControl("Calendar1");
                Date = Calendar1.SelectedDate;
                ViewState["UserId"] = UserId;
                ViewState["Date"] = Date;

                GoogleMapForASPNet1.GoogleMapObject.APIKey = ConfigurationManager.AppSettings["GoogleAPIKey"];
                GoogleMapForASPNet1.GoogleMapObject.APIVersion = "2";
                GoogleMapForASPNet1.GoogleMapObject.Width = "590px";
                GoogleMapForASPNet1.GoogleMapObject.Height = "489px";
                GoogleMapForASPNet1.GoogleMapObject.ZoomLevel = 12;
                GoogleMapForASPNet1.GoogleMapObject.AutomaticBoundaryAndZoom = false;
                GooglePoint GP = new GooglePoint("1", 30.59, 114.30);
                GoogleMapForASPNet1.GoogleMapObject.CenterPoint = GP;
                GoogleMapForASPNet1.GoogleMapObject.Points.Add(GP);

                Bind();
            }
            else if (IsPostBack)
            {
                UserId = (Guid)ViewState["UserId"];
                Date = (DateTime)ViewState["Date"];
            }
            else
                Response.Redirect("AssignSiteStep1.aspx");
            litDate.Text = Date.ToLongDateString();            
        }

        private void Bind()
        {
            //未分配点绑定          
            SiteListUnused.DataTextField = "Name";
            SiteListUnused.DataValueField = "Id";
            SiteListUnused.DataSource = Sites.GetUnassignedSitesOnSomeDay(Date);
            SiteListUnused.DataBind();

            //已分配点绑定
            SiteListUsed.DataTextField = "Name";
            SiteListUsed.DataValueField = "Id";
            SiteListUsed.DataSource = Sites.GetAssignedSites(UserId, Date);
            SiteListUsed.DataBind();
        }

        protected void AreaList_SelectedIndexChanged(object sender, EventArgs e)
        {
            //refresh two list according to the selection
        }

        protected void Submit_Click(object sender, EventArgs e)
        {
            Records.AddRecord(Date, UserId, int.Parse(SiteListUnused.SelectedValue), int.Parse(AreaList.SelectedValue));
            Response.Redirect("AssignSiteStep1.aspx");
            
        }

        private void MapRecenter(GooglePoint point)
        {
            GoogleMapForASPNet1.GoogleMapObject.CenterPoint = point;
            GoogleMapForASPNet1.GoogleMapObject.RecenterMap = true;
        }

        protected void SiteListUnused_SelectedIndexChanged(object sender, EventArgs e)
        {
            Site site = Sites.GetSite(int.Parse(SiteListUnused.SelectedValue));
            GooglePoint GP = GoogleMapForASPNet1.GoogleMapObject.Points["1"];
            GP.Latitude = site.Latitude;
            GP.Longitude = site.Longitude;
            MapRecenter(GoogleMapForASPNet1.GoogleMapObject.Points["1"]);
        }
    }
}
